package com.qitianyong.adapter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gkusbsdk.GKUSBCommand;
import com.gkusbsdk.GKUSBFileInfo;
import com.gkusbsdk.GKUSBRecordDate;
import com.qitianyong.animator.CarAmimator;
import com.qitianyong.selfclass.CONSTANT;
import com.qitianyong.selfclass.CustomApp;
import com.qitianyong.selfclass.PublicMethod;
import com.qitianyong.sgkusbcamera.R;

import java.util.List;

import static com.qitianyong.sgkusbcamera.GalleryActivity.MAX_LINE_ITEMS;

/**
 * Created by qitianyong on 2017/3/21.
 */

public class RecycleAdapter extends  RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private static final int TYPE_DATE = 0;
    private static final int TYPE_ITEM = 1;
    private static final int TYPE_FOOTER = 2;

    private Activity __Context;
    private List __list;
    private int __mode = CONSTANT.FILELIST_MODE.MODE_PLAY;
    private int __status_load = CONSTANT.LOAD_STATUS.STATUS_FINISH;
    private int __selected_count = 0;
    private IClick __iClick;

    public interface IClick{
        void onItemClick(View v, int position, int mode, int select_count);
    }

    public void setClickCallback(IClick iClick){
        __iClick = iClick;
    }

    // 构造函数
    public RecycleAdapter(Activity context, List list){

        __Context = context;
        __list = list;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof ItemDateHolder){ // 日期的处理

            GKUSBRecordDate date = (GKUSBRecordDate)__list.get(position);
            ItemDateHolder dateHolder = (ItemDateHolder)holder;

            String strDate = ""+date.__year+__Context.getString(R.string.year) +
                    date.__month+__Context.getString(R.string.month)+date.__day+
                    __Context.getString(R.string.day);
            dateHolder.__tv_date.setText(strDate);

        }else if(holder instanceof ItemViewHolder){ // 文件项

            GKUSBFileInfo fileInfo = (GKUSBFileInfo)__list.get(position);
            ItemViewHolder fileHolder = (ItemViewHolder)holder;
            fileHolder.setPostion(position);

            if(fileInfo.__type == GKUSBCommand.FILE_TYPE.HURRY_FILE){ // 紧急视频
                fileHolder.__iv_type_icon.setVisibility(View.VISIBLE);
            }else{
                fileHolder.__iv_type_icon.setVisibility(View.INVISIBLE);
            }

            String key = "";
            if(fileInfo.__type == GKUSBCommand.FILE_TYPE.PHOTO_FILE){
                fileHolder.__iv_videos_icon.setVisibility(View.INVISIBLE);
                fileHolder.__tv_timelong.setVisibility(View.INVISIBLE);
                if(fileInfo.__name!=null) {
                    key = new String(fileInfo.__name);
                }
                fileHolder.__iv_smallbitmap.setTag(key);
            }else{
                if(fileInfo.__thumb_name!=null){
                    key = new String(fileInfo.__thumb_name);
                }
                fileHolder.__iv_smallbitmap.setTag(fileInfo.__thumb_name);
                fileHolder.__iv_videos_icon.setVisibility(View.VISIBLE);
                fileHolder.__tv_timelong.setVisibility(View.VISIBLE);
            }
            int w = fileHolder.__iv_smallbitmap.getContext().getResources().getDisplayMetrics().widthPixels / MAX_LINE_ITEMS;
            Bitmap asBitmap = CustomApp.__lruCacheHelper.getAsBitmap(key, w, w);
            if(asBitmap != null){
                fileHolder.__iv_smallbitmap.setImageBitmap(asBitmap);
            }else {
                fileHolder.__iv_smallbitmap.setImageResource(R.mipmap.img_video_list_small_bg);

            }
            int minute = fileInfo.__duration/60000;
            int second = fileInfo.__duration/1000%60;

            String time = PublicMethod.dataTo2Char(minute)+":"+
                    PublicMethod.dataTo2Char(second);
            fileHolder.__tv_timelong.setText(time);

            GKUSBRecordDate date=PublicMethod.parseString2RecordDate(new String(fileInfo.__name));
            String filetime= PublicMethod.dataTo2Char(date.__hour)+":"+
                    PublicMethod.dataTo2Char(date.__minute)+":"+
                    PublicMethod.dataTo2Char(date.__second);
            fileHolder.__tv_time.setText(filetime);


            if(__mode == CONSTANT.FILELIST_MODE.MODE_SELECTOR){
                if(fileInfo.__select){
                    fileHolder.__iv_selector.setVisibility(View.VISIBLE);
                }else{
                    fileHolder.__iv_selector.setVisibility(View.INVISIBLE);
                }
            }else{
                fileHolder.__iv_selector.setVisibility(View.INVISIBLE);
                fileInfo.__select =  false;
            }

        }else if(holder instanceof FootViewHolder){// 底部
            // 底部刷新处理
            FootViewHolder footHolder = (FootViewHolder)holder;
            if(__status_load == CONSTANT.LOAD_STATUS.STATUS_LOADING) {

                CarAmimator.startBufferAnimator(16, "loader_", __Context, footHolder.__iv_animate,30);
                footHolder.__tv_tip.setText(__Context.getString(R.string.up_loading));
            }else if(__status_load == CONSTANT.LOAD_STATUS.STATUS_NODATA){

                CarAmimator.stopBufferAnimator(footHolder.__iv_animate);
                footHolder.__tv_tip.setText(__Context.getString(R.string.up_nomore));
            }
        }
    }

    @Override
    public int getItemCount() {

        if(__status_load == CONSTANT.LOAD_STATUS.STATUS_FINISH){
            Log.i("footer", "refresh count finish------");
            return __list.size();
        }

        Log.i("footer", "refresh count------");
        return __list.size() == 0 ? 0 : __list.size() + 1;
    }

    public int getCurrentSelectedCount(){
        return __selected_count;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(__Context).inflate(R.layout.item_file, parent,
                    false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER ) {
            View view = LayoutInflater.from(__Context).inflate(R.layout.loading_foot, parent,
                    false);
            return new FootViewHolder(view);
        }else if(viewType == TYPE_DATE){
            View view = LayoutInflater.from(__Context).inflate(R.layout.item_date, parent, false);
            return new ItemDateHolder(view);
        }

        return null;
    }

    @Override
    public int getItemViewType(int position) {

        if(position+1 >= getItemCount() &&
                __status_load != CONSTANT.LOAD_STATUS.STATUS_FINISH){
            Log.i("footer","---add footer index:"+position+" item count:"+getItemCount()+" list size:"+__list.size());
            return TYPE_FOOTER;
        }else{
            Object obj = __list.get(position);
            if(obj instanceof GKUSBRecordDate){
                Log.i("footer","---add date index:"+position+" item count:"+getItemCount()+" list size:"+__list.size());
                return TYPE_DATE;
            }

            Log.i("footer","---add item index:"+position+" item count:"+getItemCount()+" list size:"+__list.size());
            return TYPE_ITEM;
        }
    }

    public void setLoadStatus(int status){
        __status_load = status;
        notifyDataSetChanged();
    }

    public void resetSelectedCount(){
        __selected_count = 0;
    }

    public void setAdapterMode(int mode){
        __mode = mode;
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        public ImageView __iv_smallbitmap;
        public ImageView __iv_videos_icon;
        public TextView __tv_timelong;
        public ImageView __iv_type_icon;
        public ImageView __iv_selector;
        public TextView __tv_time;

        private int __position = 0;

        public ItemViewHolder(View view){
            super(view);

            __iv_smallbitmap = (ImageView)view.findViewById(R.id.iv_smallbitmap);
            __iv_videos_icon = (ImageView)view.findViewById(R.id.iv_videos_icon);
            __tv_timelong = (TextView)view.findViewById(R.id.tv_timelong);
            __iv_type_icon = (ImageView)view.findViewById(R.id.iv_type_icon);
            __iv_selector = (ImageView)view.findViewById(R.id.iv_chioce);
            __tv_time = (TextView)view.findViewById(R.id.tv_time);

            __iv_smallbitmap.setOnClickListener(this);
        }

        public void setPostion(int pos){
            __position = pos;
        }

        @Override
        public void onClick(View v) {
            if(__mode == CONSTANT.FILELIST_MODE.MODE_SELECTOR){

                GKUSBFileInfo fileInfo = (GKUSBFileInfo)__list.get(__position);
                fileInfo.__select = !fileInfo.__select;

                if(fileInfo.__select){
                    __selected_count++;
                }else{
                    __selected_count--;
                }

                notifyDataSetChanged();
            }else if(__mode == CONSTANT.FILELIST_MODE.MODE_PLAY){

            }

            __iClick.onItemClick(v, __position, __mode, __selected_count);
        }
    }

    public class ItemDateHolder extends RecyclerView.ViewHolder{

        public TextView __tv_date;

        public ItemDateHolder(View itemView) {
            super(itemView);

            __tv_date = (TextView)itemView.findViewById(R.id.textview_date);
        }
    }

    public class FootViewHolder extends RecyclerView.ViewHolder{

        public ImageView __iv_animate;
        public TextView __tv_tip;

        public FootViewHolder(View itemView) {
            super(itemView);

            __iv_animate = (ImageView)itemView.findViewById(R.id.iv_animate);
            __tv_tip = (TextView)itemView.findViewById(R.id.textview_tip);
        }
    }
}
